package j2025.j03;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @Author 猫大仙
 */
public class j0308 {
 public int[][] merge(int[][] intervals) {
  int m = intervals.length,n = intervals[0].length;
  Arrays.sort(intervals,(a,b)->{
   return a[0]-b[0];
  });
  ArrayList<int[]> ret = new ArrayList<>();
  int left = intervals[0][0],right = intervals[0][1];
  for (int i = 1; i < m; i++) {
   int l = intervals[i][0],r = intervals[i][1];
   //判断是否重合
   if(right>=l){
    right = Math.max(r,right);
   }else {
    ret.add(new int[]{left,right});
    left = l;right = r;
   }
  }
  ret.add(new int[]{left,right});
  return ret.toArray(new int[ret.size()][]);
  }
}
